<template>
    <section id="o_invoice">
      <ma-header :titleName = titleName >
        <div slot="title-right" class="title-right" v-tap="{methods:save}">保存</div>
      </ma-header>
      <div class="invoive-list">
        <cells>
          <cell v-for="item in invoiveArr" 
                :value="item.value"
                v-tap="{methods:add,value:item}"
                :link="item.value === ''"
                :key="item.name">
            <p slot="label">{{item.name}}</p>
          </cell>
        </cells>
      </div>
      <div class="person-info">
        <cells>
          <cell v-for="person in personInfo" 
                :value="person.value"
                v-tap="{methods:add,value:person}"
                :link="person.value === ''"
                :key="person.name">
            <p slot="label">{{person.name}}</p>
          </cell>
        </cells>
      </div>
      <input-information :value="inputData" v-on:control="control" v-show="inputShow"/>
    </section>
</template>
<script>
import maHeader from 'components/magicHeader'
import cells from 'components/cells'
import cell from 'components/cell'
import inputInformation from 'components/input-information'
import oInvoiceData from 'services/orders/o_invoice'
// import UpdateInvoiceService from "services/orders/UpdateInvoice";

export default {
  name:'o_invoice',
  components: {
    maHeader,
    cells,
    cell,
    inputInformation
  },
  data(){
    return{
      invoiveArr:oInvoiceData.invoiveArr,
      personInfo:oInvoiceData.personInfo,
      inputData:{},       // 输入框的数据
      inputShow:false,    // 判断输入组件是否显示
      isUpdate: 'false', // 判断是否是更新
      invoiceId :'' , //发票ID
      titleName:'编辑发票信息'
    }
  },
  created () {
    if(this.$route.query.invoice === undefined){
      this.titleName = '添加发票信息'
    }
    let _invoive = this.$route.query.invoice || false
    this.isUpdate = _invoive ? 'true' : 'false'
    console.log(_invoive)
    if(_invoive){
      _invoive = JSON.parse(_invoive)
      this.invoiveArr.forEach(item=>{
        item.value = _invoive[item.fullName]
      })
      this.personInfo.forEach(item=>{
        item.value = _invoive[item.fullName]
      })
      this.invoiceId = _invoive.id
    }else{
      console.log(this.invoiveArr)
      this.invoiveArr.forEach(item=>{
        item.value = ''
      })
      this.personInfo.forEach(item=>{
        item.value = ''
      })
    }
  },
  methods: {
    // 控制输入框的显示隐藏
    control(){
      this.inputShow = !this.inputShow
    },
    // 修改信息
    add(params){
      let _value = params.value
      if(_value.title==''){
        this.$.push("route_location", null, resp => {
          try {
            _value.value = resp === "---" ? "" : resp;
          } catch (error) {
            window.alert(error);
          }
        });
        return
      }
      this.inputData = _value
      this.control()
      
    },
    //保存
    save(){
      var _saveArr = this.invoiveArr.concat( this.personInfo )
      let _saveData = {invoice:{}}
      let vm = this
      _saveArr.forEach(item=>{
        _saveData.invoice[item.fullName] = item.value
      })
      console.log(_saveData)
      if(vm.judgeInfoComplete(_saveArr)){
        if(vm.isUpdate === 'true'){
          _saveData.invoice.id = vm.invoiceId
          _saveData.methodName = 'UpdateInvoice'
          oInvoiceData.updateInvoice.bind(vm)(_saveData,resp=>{
            console.log('保存成功')
            vm.$router.go(-1)
          },error=>{
            vm.$.toast(error)
          })
        }else{
          _saveData.methodName = 'AddInvoice'
          oInvoiceData.updateInvoice.bind(vm)(_saveData,resp=>{
            console.log('添加成功')
            vm.$router.go(-1)
          },error=>{
            vm.$.toast(error)
          })
        }
      }
    },
    // 判断信息填写是否完整
    judgeInfoComplete(params) {
      let flag = true
      params.forEach(value=>{
        if(value.errorText){
          if(flag &&value.value === ''){
            this.$.toast(value.errorText)
            flag = false
          }
        }
      })
      return flag;
    },
    upData(){
      oInvoiceData.updateInvoice.bind(this).then(resp=>{

      },(error)=>{
        console.log(error)
      })
    }
  }
}
</script>
<style lang="scss">
#o_invoice{
  .title-right{
    color: #ff8d00;
    font-size: 16px;
  }
  .invoive-list{
    padding-top: 10px;
  }
  .person-info{
    padding-top: 20px;
  }
}
</style>


